/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Endlesslove
 */
public class ChiTietPhieuNhap implements IDaTa{

    @Override
    public ResultSet Search(DTO.Idata Obj) 
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  ((DTO.ChiTietPhieuNhap)Obj).getMaPhieuNhap()) ;//masach
            return DAO.CallProc("LayThongTinChiTietPhieuNhapSach", params);
            
        } 
        catch (Exception ex) 
        {
            return null;
        }
    }

    @Override
    public boolean Update(DTO.Idata Obj) 
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean Delete(DTO.Idata Obj) 
    {
        List<String> params = new ArrayList<>();
        params.add("char:" +  ((DTO.ChiTietPhieuNhap)Obj).getID()) ;//ma chi tiet
        return DAO.CallProc("XoaChiTietPhieuNhap", params, true);
    }

    @Override
    public void LoadData(Object table) 
    {
       
    }

    @Override
    public boolean Insert(DTO.Idata Obj) 
    {
        List<String> params = new ArrayList<>();
        params.add("char:" +  ((DTO.ChiTietPhieuNhap)Obj).getID()) ;//ma chi tiet
        params.add("char:" +  ((DTO.ChiTietPhieuNhap)Obj).MaPhieuNhap);//ma phieu nhap
        params.add("char:" +  ((DTO.ChiTietPhieuNhap)Obj).MaSach );//ma sach
        params.add("smallint:" +  ((DTO.ChiTietPhieuNhap)Obj).SoLuongNhap );//so luong nhap
        params.add("int:" + ((DTO.ChiTietPhieuNhap)Obj).DonGiaNhap);//don gia
        return DAO.CallProc("ThemChiTietPhieuNhap", params, true);
    }

    @Override
    public String GenerateID() 
    {
        try 
        {
                ResultSet res = DAO.CallProc("LayMaChiTietPhieuNhapSachCuoi");
                if (res.next())
                {  
                    try
                    {
                        int index  = Integer.parseInt(((String)res.getObject(1)).split("CTPN")[1].trim());
                        String mkh = "CTPN";
                        mkh += (index+1)>=10?(index+1)>=100? (index +1) : "0" + (index +1) : "00" + (index + 1);
                        return mkh;
                    }
                    catch(Exception ie)
                    {
                        return "CTPN001";
                    }
                }
                return "CTPN001";
        } 
        catch (SQLException ex) 
        {
            return "CTPN001";
        }
    }
    
    public ResultSet GetCTPN(String MPN)
    {
         try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  MPN) ;//masach
            return DAO.CallProc("LayThongTinChiTietPhieuNhapSach", params);
            
        } 
        catch (Exception ex) 
        {
            return null;
        }
    }
}
